package o;

import android.icu.util.ULocale;
import android.os.Build.VERSION;
import android.util.Log;
import androidx.annotation.Nullable;
import java.lang.reflect.Method;
import java.util.Locale;

public final class gs {
    private static Method ˎ;
    private static Method ॱ;

    static {
        if (VERSION.SDK_INT < 21) {
            try {
                Class cls = Class.forName("libcore.icu.ICU");
                if (cls != null) {
                    ॱ = cls.getMethod("getScript", new Class[]{String.class});
                    ˎ = cls.getMethod("addLikelySubtags", new Class[]{String.class});
                }
            } catch (Throwable e) {
                ॱ = null;
                ˎ = null;
                Log.w("ICUCompat", e);
            }
        } else if (VERSION.SDK_INT < 24) {
            try {
                ˎ = Class.forName("libcore.icu.ICU").getMethod("addLikelySubtags", new Class[]{Locale.class});
            } catch (Throwable e2) {
                throw new IllegalStateException(e2);
            }
        }
    }

    @Nullable
    public static String ˊ(Locale locale) {
        if (VERSION.SDK_INT >= 24) {
            return ULocale.addLikelySubtags(ULocale.forLocale(locale)).getScript();
        }
        if (VERSION.SDK_INT >= 21) {
            try {
                return ((Locale) ˎ.invoke(null, new Object[]{locale})).getScript();
            } catch (Throwable e) {
                Log.w("ICUCompat", e);
                return locale.getScript();
            } catch (Throwable e2) {
                Log.w("ICUCompat", e2);
                return locale.getScript();
            }
        }
        String ॱ = ॱ(locale);
        if (ॱ != null) {
            return ˎ(ॱ);
        }
        return null;
    }

    private static String ˎ(String str) {
        try {
            if (ॱ != null) {
                return (String) ॱ.invoke(null, new Object[]{str});
            }
        } catch (Throwable e) {
            Log.w("ICUCompat", e);
        } catch (Throwable e2) {
            Log.w("ICUCompat", e2);
        }
        return null;
    }

    private static String ॱ(Locale locale) {
        String locale2 = locale.toString();
        try {
            if (ˎ != null) {
                return (String) ˎ.invoke(null, new Object[]{locale2});
            }
        } catch (Throwable e) {
            Log.w("ICUCompat", e);
        } catch (Throwable e2) {
            Log.w("ICUCompat", e2);
        }
        return locale2;
    }
}
